// window.onload = function(){
function sum(arr) {
   var s = 0;
    for (var i=arr.length-1; i>=0; i--) {
        s += arr[i];
    }
    return s;
}
var token=localStorage.getItem('token');


if(sel_id = getUrlParam('sel_id')){
    var sel_id_arr = sel_id.split( ',' );
}

// url网站域名
var url="http://hengzhi.ayvvv.com";
         

// 购物车结果加载开始
$.ajax({
    url:url+'/api/user/cart/lst',
    dataType:"json",
    type:"get",
    headers:{
    "X-Token":token,
    },
    async:true,
    success:function(result){
        console.log(sel_id_arr);
        console.log(result);
        var all_list=result.data.list;
        let allpirce=[];
        let allcate_id=[];
        for (var i = 0; i < all_list.length; i++) {
            var store_list=all_list[i].list;
            for (var j = 0; j < store_list.length; j++) {
                for (var k = 0; k < sel_id_arr.length; k++) {
                    if (sel_id_arr[k]==store_list[j].cart_id){
                        var subtotal=store_list[j].product.price*store_list[j].cart_num;
                        $('.store_car').append(`<tr calss="sel_id_str">
                                <td class="col1"><a href=""><img src="`+store_list[j].product.image+`" alt="`+store_list[j].cart_id+`" /></a>  <strong><a href="">`+store_list[j].product.store_name+`</a></strong></td>
                                <td class="col2"> <p>`+store_list[j].productAttr.sku+`</p> </td>
                                <td class="col3">￥`+store_list[j].product.price+`</td>
                                <td class="col4"> `+store_list[j].cart_num+`</td>
                                <td class="col5"><span>￥`+subtotal+`</span></td>
                        </tr>`);
                        allpirce.push(subtotal);
                        allcate_id.push(store_list[j].cart_id); 
                    }
                          
                }
             


            }

        };


        $('.allpirce').text(Math.round(sum(allpirce)*100)/100);
        var allpir=Math.round(sum(allpirce)*100)/100;
        // 收货地址加载开始
        let alladd_id=[];
        $.ajax({
            url:url+'/api/user/address/lst',
            dataType:"json",
            type:"get",
            headers:{
            "X-Token":token,
            },
            async:true,
            success:function(result){
                console.log(result.data.list)
                $('.address_info').append(`<p>`+`联系方式 `+result.data.list[0].phone+`</p><p title="`+result.data.list[0].address_id+`">`+result.data.list[0].province+' '+result.data.list[0].city+' '+result.data.list[0].district+' '+result.data.list[0].detail+`</p>`);
                var address_arr=result.data.list;
                for (var i = 0; i < address_arr.length; i++) {
                    $('.alladdress').append(`<li class="cur">
                                    <input type="radio" name="address" checked="checked" value="`+result.data.list[0].address_id+`" />`+result.data.list[i].phone+' '+result.data.list[i].province+' '+result.data.list[i].city+' '+result.data.list[i].district+' '+result.data.list[i].detail+`
                                    <a href="">设为默认地址</a>
                                    <a href="">编辑</a>
                                    <a href="">删除</a>
                                </li>`)
                }
                alladd_id.push(result.data.list[0].address_id);
            },
            rror:function(){
                alert('请稍后重试');
            }

        })
        // 收货地址加载结束   
        // 商品数量加载开始
        $.ajax({
            url:url+'/api/user/cart/count',
            dataType:"json",
            type:"get",
            headers:{
            "X-Token":token,
            },
            async:true,
            success:function(count){ 
                $('#store_num').text(count.data[0].count+'件商品，总商品金额：￥'); 
            },
            error:function(){
                
            }
            })
        // 商品数量加载结束
        let freightpirce=[];
        var frepir=0;
        // 确认订单开始          
        $(".qrdd").click(function(){
                $.ajax({
                    url:url+'/api/order/create',
                    dataType:"json",
                    type:"post",
                    data:{"cart_id": allcate_id,"address_id": alladd_id[0],"pay_type": "h5"},
                    headers:{
                    "X-Token":token,
                    },
                    async:true,
                    success:function(result){
                        if (result.status==200) {
                            window.location.href=`paymoney.html?nop=`+result.data.result.order_id;
                        }else{
                            window.location.href="login.html";
                        }
                        
                        
                            
                    },
                    error:function(){
                        
                    }
                    })
        })
        // 确认订单结束

        var allpirces=allpir+frepir;
        $('.allpirces').text('应付总额：￥'+allpirces+'元');

    },
    error:function(){
        alert('请稍后重试')
    }
    })
// 购物车结果加载结束   













































// }